package cn.midas.org.controller;

import cn.midas.basic.utils.AjaxResult;
import cn.midas.basic.utils.PageList;
import cn.midas.org.domain.Department;
import cn.midas.org.query.DepartmentQuery;
import cn.midas.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department loadById(@ApiParam(value="t_department表主键",required = true) @PathVariable("id") Long id) {
        return departmentService.loadById(id);
    }

    @GetMapping
    public List<Department> loadAll() {
        return departmentService.loadAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage("系统繁忙,请稍后重试!");
        }
    }

    @PutMapping
    public AjaxResult uptOrAdd(@RequestBody Department department) {
        try {
            if (department.getId()==null) {
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage("系统繁忙,请稍后重试!");
        }


    }
    @PostMapping
    public PageList<Department> queryData(@RequestBody DepartmentQuery departmentQuery) {
            return departmentService.queryData(departmentQuery);
    }

}